package com.ycsj.goldmedalnewconcept.master.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.ycsj.common.utils.GsonUtil;
import com.ycsj.common.utils.SPUtil;
import com.ycsj.common.view.dialog.AlertDialogUtil;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.master.adapter.DiyRedpackageAdapter;
import com.ycsj.goldmedalnewconcept.master.bean.DiyRedpackageInfo;
import com.ycsj.goldmedalnewconcept.master.bean.RedPackageInfo;
import java.util.ArrayList;
import java.util.List;

/**
 * 红包定制方案
 */

@SuppressLint("NewApi")
public class MasterDiyRedPackageActivity extends Activity implements
        OnClickListener {

    private ListView lv_diy;

    private DiyRedpackageAdapter mAdapter;

    private List<RedPackageInfo> mDatas;

    private TextView tv_addred;

    private LinearLayout tv_cancel;

    private Button bt_creat;

    private RedPackageInfo info;

    private AlertDialog alertDialog;

    private Window window;

    private int dialogWidth = 550;

    private int dialogHeight = 260;

    private WindowManager.LayoutParams lp;

    private TextView dialog_creat;

    private TextView dialog_cancel;

    private Display mDisplay;

    private EditText et_money;

    private EditText et_number;

    private RelativeLayout rl_null;

    private TextView tv_moneys;

    private TextView tv_count;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_diy_redpackage);
//		Res.init(this);

        // 获取屏幕尺寸
        getPhoneWindow();

        // 设置状态栏

        // 初始化View
        initView();

        // 设置数据
        initdata();

        // 设置监听
        initListener();

    }

    /**
     * 获取屏幕尺寸
     */
    private void getPhoneWindow() {

        mDisplay = getWindowManager().getDefaultDisplay();

        int W = mDisplay.getWidth();

        int H = mDisplay.getHeight();

        dialogWidth = W * 3 / 4;

        dialogHeight = dialogWidth * 3 / 4;

    }

    /**
     * 初始化监听
     */
    private void initListener() {

        tv_cancel.setOnClickListener(this);

        tv_addred.setOnClickListener(this);

        bt_creat.setOnClickListener(this);

    }

    /**
     * 初始化View的方法
     */
    private void initView() {

        tv_cancel = findViewById(R.id.tv_cancel);

        lv_diy = findViewById(R.id.lv_diy);

        tv_addred = findViewById(R.id.tv_addred);

        tv_moneys = findViewById(R.id.tv_moneys);

        tv_count = findViewById(R.id.tv_count);

        bt_creat = findViewById(R.id.bt_creat);

        rl_null = findViewById(R.id.rl_null);

    }

    /**
     * 初始化数据
     */
    private void initdata() {

        mDatas = new ArrayList<RedPackageInfo>();
//		mDatas = new ArrayList<RedPackageInfo>();

        String diyJson = getIntent().getStringExtra("diyJson");

        if (diyJson != null && !TextUtils.isEmpty(diyJson)) {
            DiyRedpackageInfo info = new DiyRedpackageInfo();
            info = GsonUtil.jsonToBean(diyJson, DiyRedpackageInfo.class);
            mDatas = info.list;

        }


        mAdapter = new DiyRedpackageAdapter(this, mDatas,
                R.layout.item_diy_redpackage);

        lv_diy.setAdapter(mAdapter);


        // 长按删除方案条目
        lv_diy.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view,
                                           final int position, long id) {

                AlertDialogUtil.showAlertDialog(true, MasterDiyRedPackageActivity.this,
                        "是否删除该定制方案", new AlertDialogUtil.onListener() {
                            @Override
                            public void onClickOk() {
                                mDatas.remove(position);

                                mAdapter.updateListView(mDatas);

                                if (mDatas.size() == 0) {

                                    lv_diy.setVisibility(lv_diy.GONE);

                                    rl_null.setVisibility(rl_null.VISIBLE);

                                } else {
                                    lv_diy.setVisibility(lv_diy.VISIBLE);

                                    rl_null.setVisibility(rl_null.GONE);
                                }

                                int moneys = 0;

                                int count = 0;

                                // 计算总金额及总个数
                                for (int i = 0; i < mDatas.size(); i++) {

                                    moneys = moneys
                                            + (mDatas.get(i).money * mDatas.get(i).number);

                                    count = count + mDatas.get(i).number;
                                }

                                tv_moneys.setText("红包总金额：   " + moneys + " 元");

                                tv_count.setText(count + " 个");

                            }

                            @Override
                            public void onClickDismiss() {

                            }
                        });
                return false;
            }
        });
    }

    /**
     * 页面控件点击事件
     *
     * @param view
     */
    @Override
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.tv_addred:
                // 添加

                if (mDatas.size() > 25) {

                    Toast.makeText(MasterDiyRedPackageActivity.this, "方案数最大为26",
                            Toast.LENGTH_SHORT).show();

                    break;
                }

                // 添加红包对话框
                addRedpackage();

                break;

            case R.id.tv_cancel:

                // 返回
                DiyRedpackageInfo info = new DiyRedpackageInfo();
                info.list = mDatas;

                String diyJson = GsonUtil.objectToJson(info);

                Intent intent = new Intent(MasterDiyRedPackageActivity.this, MasterCreatRedPackageActivity.class);

                SPUtil.put(MasterDiyRedPackageActivity.this, "diyJson", diyJson);
                startActivity(intent);
                finish();

                break;

            case R.id.bt_creat:

                // 确定
                DiyRedpackageInfo info1 = new DiyRedpackageInfo();
                info1.list = mDatas;

                String diyJson1 = GsonUtil.objectToJson(info1);

                Intent intent1 = new Intent(MasterDiyRedPackageActivity.this, MasterCreatRedPackageActivity.class);

                SPUtil.put(MasterDiyRedPackageActivity.this, "diyJson", diyJson1);

                startActivity(intent1);

                finish();
                break;
        }
    }

    /**
     * 添加红包弹窗
     */
    public void addRedpackage() {

        alertDialog = new AlertDialog.Builder(this).create();

        alertDialog.show();

        window = alertDialog.getWindow();

        lp = window.getAttributes();

        lp.x = 0;

        window.setContentView(R.layout.dialog_diy_redpackage);

        alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable());

        alertDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

        window.setLayout(dialogWidth, dialogHeight);

        dialog_cancel = (TextView) alertDialog.getWindow().findViewById(
                R.id.tv_cancel);

        dialog_creat = (TextView) alertDialog.getWindow().findViewById(
                R.id.tv_creat);

        et_money = (EditText) alertDialog.getWindow().findViewById(
                R.id.et_money);

        et_number = (EditText) alertDialog.getWindow().findViewById(
                R.id.et_number);

        // 取消键
        dialog_cancel.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });

        // 添加键
        dialog_creat.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (TextUtils.isEmpty(et_money.getText().toString().trim())
                        || "0".equals(et_money.getText().toString().trim())) {

                    Toast.makeText(MasterDiyRedPackageActivity.this, "请输入红包金额",
                            Toast.LENGTH_SHORT).show();

                    return;
                }

                if (TextUtils.isEmpty(et_number.getText().toString().trim())
                        || "0".equals(et_number.getText().toString().trim())) {

                    Toast.makeText(MasterDiyRedPackageActivity.this, "请输入红包个数",
                            Toast.LENGTH_SHORT).show();

                    return;
                }

                int money = Integer.parseInt(et_money.getText().toString()
                        .trim());

                int number = Integer.parseInt(et_number.getText().toString()
                        .trim());

                if (money == 0 || number == 0) {

                    Toast.makeText(MasterDiyRedPackageActivity.this, "请先完善红包方案",
                            Toast.LENGTH_SHORT).show();

                    return;
                }

                int tempMoneys = 0;

                int tempnumbers = 0;

                for (int i = 0; i < mDatas.size(); i++) {

                    tempMoneys = tempMoneys
                            + (mDatas.get(i).money * mDatas.get(i).number);

                    tempnumbers = tempnumbers + mDatas.get(i).number;
                }

                tempMoneys = tempMoneys + (money * number);

                tempnumbers = tempnumbers + number;

                if (tempMoneys > 100000000 || (money * number) < 0) {
                    Toast.makeText(MasterDiyRedPackageActivity.this, "总金额不能超过1亿",
                            Toast.LENGTH_SHORT).show();

                    return;
                }
                if (tempnumbers > 9999) {
                    Toast.makeText(MasterDiyRedPackageActivity.this, "红包总个数超限",
                            Toast.LENGTH_SHORT).show();

                    return;
                }

                info = new RedPackageInfo(money, number);

                mDatas.add(info);

                if (mDatas.size() == 0) {

                    lv_diy.setVisibility(lv_diy.GONE);

                    rl_null.setVisibility(rl_null.VISIBLE);

                } else {

                    lv_diy.setVisibility(lv_diy.VISIBLE);

                    rl_null.setVisibility(rl_null.GONE);

                }

                mAdapter.updateListView(mDatas);

                int moneys = 0;

                int count = 0;

                for (int i = 0; i < mDatas.size(); i++) {
                    moneys = moneys
                            + (mDatas.get(i).money * mDatas.get(i).number);

                    count = count + mDatas.get(i).number;
                }

                tv_moneys.setText("红包总金额：   " + moneys + " 元");

                tv_count.setText(count + " 个");

                alertDialog.dismiss();
            }
        });
    }

}
